package cn.edu.glut.choosethesis.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.glut.choosethesis.service.SelectionService;
import cn.edu.glut.dto.Response;
import cn.edu.glut.dto.UserIntro;
import cn.edu.glut.user.service.UserDetailService;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月18日下午4:18:07
 * @email Caizhfy@163.com
 * @description 该控制器维护选题操作
 *
 */
@RestController
@RequestMapping("/selection")
public class SelectionController {
	@Autowired
	SelectionService selectionService;
	
	//教师确选某个题目
	@RequestMapping(value = "/teach/choice")
	public Response selection(@RequestParam("studentId") String studentId,@RequestParam("subjectId") String subjectId){
		return Response.success(selectionService.updateSelectStatus(studentId, subjectId));
	}
	
	//内定
	@RequestMapping("/privacy/choice")
	public Response privacyChoice(@RequestParam("username") String stuUsername,@RequestParam("subjectId") String subjectId){
		return Response.success(selectionService.privacyChoice(stuUsername, subjectId));
	}
	
	//学生选题
	/**
	 * 
	 * @param subjectId	
	 * @param wish 志愿，1为第一志愿， 2为第二志愿
	 * @param req
	 * @return
	 */
	@RequestMapping("/stu/choice")
	public Response save(@RequestParam("subjectId") String subjectId,@RequestParam("wish")int wish, HttpServletRequest req){
		HttpSession session = req.getSession();
		UserIntro u = (UserIntro) session.getAttribute("user");
		if(null!=u && u.getUserType()==UserDetailService.TYPE_STUDENT)
			return Response.success(selectionService.studentChoiceSubject(u.getId(), subjectId, wish));
		return Response.success();
	}

	
	//根据session来查询该学生下的选题详情
	//studentId只会在管理员查询的情况下传入
	@RequestMapping("/list/stu")
	public Response list(HttpServletRequest req, String studentId){
		HttpSession session = req.getSession();
		UserIntro u = (UserIntro) session.getAttribute("user");
		if(null!=u && u.getUserType() == UserDetailService.TYPE_STUDENT)
			return Response.success(selectionService.findByStudentId(u.getId()));
		else if(!StringUtils.isEmpty(studentId))
			return Response.success(selectionService.findByStudentId(studentId));
		return Response.success();
	}
	
	@RequestMapping("/refund/{subjectId}")
	public Response refund(@PathVariable String subjectId, HttpSession session){
		UserIntro u = (UserIntro) session.getAttribute("user");
		if(null!=u && u.getUserType()==UserDetailService.TYPE_STUDENT){
			return Response.success(selectionService.refund(u.getId(), subjectId));
		}
		return Response.success();
	}
}
